//
//  DTBizUtilAction.m
//  PersonalManager
//
//  Created by user on 16/8/15.
//  Copyright © 2016年 Haiyun.Qian. All rights reserved.
//

#import "DTBizUtilAction.h"
#import "ScanViewController.h"

@interface DTBizUtilAction ()

@end

@implementation DTBizUtilAction

/** 自定义配合 DTJavascriptCommand 初始化方法 */
- (instancetype)initWithCommand:(DTJavascriptCommand *)command {
    self = [super init];
    self.command = command;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showScanResult:) name:@"scanResult" object:nil];
    return self;
}

- (void)scan {
    NSString *codeType = self.command.params[@"type"];
    ScanViewController *scanVC = [[ScanViewController alloc] initWithString:codeType];
    [self.command.controller.navigationController pushViewController:scanVC animated:YES];
}

- (void) showScanResult:(NSNotification *)notification {
    NSString *string = notification.userInfo[@"result"];
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:string, @"扫描结果", nil];
        [self.command successRessponse:dic];
    }];
    [alertVC addAction:action];
    [self.command.controller presentViewController:alertVC animated:YES completion:nil];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
